package com.action;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.dto.EmpInfo;
import com.dto.Role;
import com.service.AdminService;
import com.util.FenYe;
import com.util.MD5Util;
import com.util.Query;
@RestController
@RequestMapping("AdminAction")
public class AdminAction {
	@Autowired
	private AdminService as;
	private static MD5Util mu=new MD5Util();
	@RequestMapping("adminLogin.do")//员工登录
	public Map<String,Object> queryEmp(EmpInfo emp,HttpSession session){
		Map<String,Object> json=new HashMap<String,Object>();
		String minwen=mu.getMD5tr(emp.getE_pwd());//凋用方法生成密文
		emp.setE_pwd(minwen);//把生成好的密文注入
		emp=as.queryEmp(emp);
		if(emp!=null) {
			json.put("miss", true);
			session.setAttribute("emp", emp);
		}else{
			json.put("miss", false);
		}
		return json;
	}
	@RequestMapping("queryListEmp.do")//查询所有员工信息
	public ModelAndView queryListEmp(EmpInfo emp,HttpSession session,FenYe fy,Query query) {
		ModelAndView mav=new ModelAndView();
		fy.setQuery(query);
		List<EmpInfo> Listemps=as.queryEmpByFy(fy);
		if(Listemps!=null){
			session.setAttribute("Listemps", Listemps);
			mav.addObject("fy", fy);
			mav.addObject("query", query);
			mav.setViewName("forward:/AgentSettings.jsp");
		}
		return mav;
	}
	@RequestMapping("queryRole.do")//查询员工权限
	public ModelAndView queryRole(Role role,HttpSession session) {
		List<Role> roles=as.queryRole(role);
		session.setAttribute("roles", roles);
		return new ModelAndView("forward:/addEmp.jsp");
	}
	@RequestMapping("addEmp.do")//添加员工信息
	public ModelAndView addEmp(EmpInfo emp) {
		String mw=mu.getMD5tr(emp.getE_pwd());
		emp.setE_pwd(mw);
		as.addEmp(emp);
		return new ModelAndView("redirect:queryListEmp.do");
	}
	@RequestMapping("queryEmpById.do")//根据ID查询员工信息
	public ModelAndView queryEmpById(String ids,Role role,HttpSession session) {
		List<Role> roles=as.queryRole(role);
		session.setAttribute("roles", roles);
		EmpInfo emps=as.queryEmpByIds(ids);
		session.setAttribute("emps", emps);
		return new ModelAndView("forward:/updateEmp.jsp");
	}
	@RequestMapping("updateEmp.do")//根据ID修改员工信息
	public ModelAndView updateEmp(EmpInfo emp) {
		String mw=mu.getMD5tr(emp.getE_pwd());
		emp.setE_pwd(mw);
		as.updateEmp(emp);
		return new ModelAndView("redirect:queryListEmp.do");
	}
	@RequestMapping("deleteEmp.do")//根据ID删除员工信息
	public ModelAndView deleteEmp(EmpInfo emp) {
		as.deleteEmp(emp.getIdss());
		return new ModelAndView("redirect:queryListEmp.do");
	}
	@RequestMapping("queryEmpByAdd.do")//注册验证员工名是否存在
	public Map<String,Object>  queryEmpByAdd(EmpInfo emp) {
		Map<String,Object> json=new HashMap<String,Object>();
		if(as.queryEmpByAdd(emp)!=null) {
			json.put("miss", true);
		}else {
			json.put("miss", false);
		}
		return json;
	}
	
	
	/*
	 *注销登录
	 */
	@RequestMapping("logout.do")
	public ModelAndView  logout(HttpSession sess) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("redirect:homePage.jsp");
		sess.invalidate();//session 失效
		return mv;
	}
	
}
